package controller

import (
	"github.com/gin-gonic/gin"
	"yak.admin/src/entity"
	"yak.admin/src/middleware"
	"yak.admin/src/module/backend/hnyj/form"
	"yak.admin/src/module/backend/hnyj/repository"
	"yak.admin/src/util"
)

func Users(c *gin.Context) {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	adminSearch := repository.UserRepository{Province: admin.Province, City: admin.City, County: admin.County, IsSuper: admin.IsSuper()}
	data, count := adminSearch.FindAll(c)
	middleware.Datalist(c, data, count)
}

func UsersExcel(c *gin.Context) {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	adminSearch := repository.UserRepository{Province: admin.Province, City: admin.City, County: admin.County, IsSuper: admin.IsSuper()}
	header, data := adminSearch.FindAllExcel(c)
	middleware.DataExcel(c, data, header)
}

func Checked(c *gin.Context) {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	f := form.UserCheckForm{Province: admin.Province, City: admin.City, County: admin.County, IsSuper: admin.IsSuper()}
	if err := util.DefaultGetValidParams(c, &f); err != nil {
		middleware.ResultFail(c, err.Error(), nil)
		return
	}
	if err := f.Checked(); err != nil {
		middleware.ResultFail(c, err.Error(), nil)
		return
	}
	middleware.ResultSuccess(c, "用户激活成功", nil)
}

func Remove(c *gin.Context) {
	admin := c.MustGet(util.AdminKey).(entity.Admin)
	f := form.UserCheckForm{Province: admin.Province, City: admin.City, County: admin.County, IsSuper: admin.IsSuper()}
	if err := util.DefaultGetValidParams(c, &f); err != nil {
		middleware.ResultFail(c, err.Error(), nil)
		return
	}
	if err := f.Del(); err != nil {
		middleware.ResultFail(c, err.Error(), nil)
		return
	}
	middleware.ResultSuccess(c, "删除成功", nil)
}


